package cic;

import java.text.DateFormat;
import java.util.Random;

public class MyTest {

	public static void main(String[] args) throws Exception {

		// Set up Information Monitor
		InforMonitor imf = new InforMonitor();
		imf.addInfor("Initializing system...............done.");

		// set up Insurance Manage System
		InsuranceManager manager = new InsuranceManager();

		// add some insurance into the Insurance Manage System
		manager.addInsurance(new Insurance("TL000001", "Antonio", 1000000, 0));
		manager.addInsurance(new Insurance("TL000002", "George", 40000, 2));
		manager.addInsurance(new Insurance("TL000003", "Ronaldo", 99990, 0));
		manager.addInsurance(new Insurance("TL000004", "Saber", 50000, 3));

		// Initialize all the employees in the company
		Employee emp = new Employee("Michael", "CD0001", Department.CarDamages,	Level.A);
		Employee emp2 = new Employee("Jackson", "CD0002", Department.CarDamages, Level.B);
		Employee emp3 = new Employee("Victoria", "FN0001", Department.Finance, Level.C);
		Employee emp4 = new Employee("Beckham", "CD0003", Department.CarDamages, Level.C);
		Employee emp5 = new Employee("Angela", "CD0004", Department.CarDamages,	Level.A);

		// Make a list of employees who are handlers
		Employee handler[] = new Employee[3];
		handler[0] = emp;
		handler[1] = emp2;
		handler[2] = emp5;

		// Display report window 
		ReportWindow rpw = new ReportWindow();
		
		while (true) {
			if (!rpw.isSubmitted()) {
				Thread.sleep(2000);
			} else {
				break;
			}
		}
		
		rpw.hide(true);
				
		
		// Create a claim
		Claim claim = new Claim(rpw.getCustomer(), rpw.getNum());
		imf.addInfor("Customer: " + rpw.getCustomer() + " reported a claim.");

		// Register the claim
		emp4.regiserClaim(claim);
		imf.addInfor("Employee: " + emp4.getID() + " " + emp4.getName()
				+ " has registered the claim.");

		// Select a handler to handle the new claim
		Random selecthandler = new Random();
		int selected = selecthandler.nextInt(2);
		imf.addInfor("Employee: " + handler[selected].getID() + " "
				+ handler[selected].getName()
				+ " is selected to be handler.");

		// Check the claim
		boolean valid = handler[selected].checkInsurance(manager, claim);
		if (valid) {
			
			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " is checking the insurance.......");
			imf.addInfor("YES ! The insurance is valid!");

			// Evaluate severity
			handler[selected].evaluate(manager, claim);
			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " is evaluating the severity of the claim......");
			
			if (claim.getCategory() == Category.simple_claim) {
				imf.addInfor("It is a simple claim.");
			} else {
				imf.addInfor("It is a complex claim.");
			}
			
			// Display the Form window
			FormWindow fmw = new FormWindow();
			
			while (true) {
				if (!fmw.isSubmitted()) {
					Thread.sleep(2000);
				} else {
					break;
				}
			}
			
			fmw.hide(true);
			
			imf.addInfor("Customer: " + rpw.getCustomer() + " has filled detail forms.");

			// If complex, check damage history
			if (claim.getCategory() == Category.complex_claim) {
				emp.checkHistory(manager, claim);
			}

			// Phone the garage
			handler[selected].phoneGarage();
			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName() + " has phoned the Garage.");

			// Make a decision
			if (handler[selected].getLevel() == Level.A) {
				handler[selected].makeDecision(claim, "OK");
				imf.addInfor("Employee: " + handler[selected].getID() + " "
						+ handler[selected].getName()
						+ " has made a positive decision!...[OK]");
			} else {
				emp.makeDecision(claim, "OK");
				imf.addInfor("Employee: " + emp.getID() + " "
						+ emp.getName()
						+ " has made a positive decision!...[OK]");
			}
			// Pay the money
			emp3.pay(claim);
			imf.addInfor("Employee: " + emp3.getID() + " " + emp3.getName()
					+ " has paid the money.");

			// then write the letter
			Letter letter = null;
			letter = handler[selected]
					.writeLetter("CONGRATULATIONS, YOUR CLAIM HAS BEEN HANDLED!");

			DateFormat df = DateFormat.getDateTimeInstance();

			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " is writing a letter to customer......");

			imf.addInfor("----------------------------------------"
					+ "----Letter--------------------------------------------");
			imf.addInfor("Handler's Name: " + letter.handlername);
			imf.addInfor("Handler's ID: " + letter.handlerid);
			imf.addInfor("DATE TIME: " + df.format(letter.date));
			imf.addInfor("CONTENT: " + letter.content);
			imf.addInfor("----------------------------------------"
					+ "----Letter--------------------------------------------");

			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " has sent the lettet to customer!");
			
			// Display letter window
			String ltcontent = new String("Hi, " + rpw.getCustomer() + "\n" + letter.content
					+ "\n" + "Handler's Name: " + letter.handlername + "\n" +
					"Handler's ID: " + letter.handlerid + "\n" +
					"DATE TIME: " + df.format(letter.date));
			
			new LetterWindow(ltcontent);

		} else {
			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " is checking the insurance.");
			imf.addInfor("NO ! The insurance is invalid!");
			// make a decision
			if (handler[selected].getLevel() == Level.A) {
				handler[selected].makeDecision(claim, "NOK");
				imf.addInfor("Employee: " + handler[selected].getID() + " "
						+ handler[selected].getName()
						+ " has made a negtive decision!...[NOK]");
			} else {
				emp.makeDecision(claim, "NOK");
				imf.addInfor("Employee: " + emp.getID() + " "
						+ emp.getName()
						+ " has made a negtive decision!...[NOK]");
			}
			// then write the letter
			Letter letter = null;
			letter = handler[selected]
					.writeLetter("SORRY, YOUR CLAIM HAS NOT BEEN ACCEPTED!");

			DateFormat df = DateFormat.getDateTimeInstance();

			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " is writing a letter to customer......");

			imf.addInfor("----------------------------------------"
					+ "----Letter--------------------------------------------");
			imf.addInfor("Handler's Name: " + letter.handlername);
			imf.addInfor("Handler's ID: " + letter.handlerid);
			imf.addInfor("DATE TIME: " + df.format(letter.date));
			imf.addInfor("CONTENT: " + letter.content);
			imf.addInfor("----------------------------------------"
					+ "----Letter--------------------------------------------");

			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " has sent the lettet to customer!");
			
			// Display letter window
			String ltcontent = new String("Hi, " + rpw.getCustomer() + "\n" + letter.content
					+ "\n" + "Handler's Name: " + letter.handlername + "\n" +
					"Handler's ID: " + letter.handlerid + "\n" +
					"DATE TIME: " + df.format(letter.date));
			
			new LetterWindow(ltcontent);

		}
		
		imf.addInfor("The processing of the insurance claim is FINISHED!");

	}

}
